<?php

namespace App\Winged\Auth\Traits;

use App\Winged\Auth\Api\Auth;
use App\Winged\Auth\Consts\Roles;
use App\Winged\Tenants\Tenant;

trait AdminPermission
{
    /**
     * Get avatar attribute.
     *
     * @param string $avatar
     *
     * @return string
     */
    public function getAvatarAttribute($avatar)
    {

        if ($avatar) {
            return rtrim(config('admin.upload.host'), '/').'/'.trim($avatar, '/');
        }

        return asset('/packages/admin/AdminLTE/dist/img/user2-160x160.jpg');
    }


    public function getName()
    {        
        return $this->getAuthIdentifierName();
    }


    

    /**
     * A user has and belongs to many roles.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function roles()
    {
        $attr = $this->getAttribute('properties') ;
        if(array_key_exists('roles' ,$attr)){
            return collect($attr ['roles']);
        }else{
            return collect([]) ;
        }
    }

    /**
     * A User has and belongs to many permissions.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function permissions()
    {
        $attr = $this->getAttribute('properties') ;
        if(array_key_exists('permissions' ,$attr)){
            return collect($attr['permissions'])->pluck('slug');
        }else{
            return collect([]) ;
        }
    }

    /**
     * Check if user has permission.
     *
     * @param $permission
     *
     * @return bool
     */
    public function can($permission)
    {
        if ($this->isAdministrator()) {
            return true;
        }
        return $this->permissions()->contains($permission);
    }

    /**
     * Check if user has no permission.
     *
     * @param $permission
     *
     * @return bool
     */
    public function cannot($permission)
    {
        return !$this->can($permission);
    }

    /**
     * Check if user is administrator.
     *
     * @return mixed
     */
    public function isAdministrator()
    {
        return $this->isRole(Roles::MASTER_SUPERADMIN);
    }

    /**
     * Check if user is $role.
     *
     * @param string $role
     *
     * @return mixed
     */
    public function isRole($role)
    {
        return $this->roles()->pluck('slug')->contains($role) ;
    }

    /**
     * Check if user in $roles.
     *
     * @param array $roles
     *
     * @return mixed
     */
    public function inRoles($roles = [])
    {
        foreach($roles as $val){
            if($this->roles()->pluck('slug')->contains($val)){
                return true ;
            }
        }
        return false;
    }

    /**
     * If visible for roles.
     *
     * @param $roles
     *
     * @return bool
     */
    public function visible($item)
    {
        if (empty($item['roles'])) {
            return true;
        }
        $roles = array_column($item['roles'], 'slug');
        $service = $item['service'];
        if (app()->tenant->hasServe($service) && ($this->inRoles($roles)  || $this->isAdministrator())) {
            return true;
        }
        return false;
    }
}
